package com.uiotsoft.small.host.controller.connect;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.uiotsoft.small.host.controller.HostBaseController;
import com.uiotsoft.small.host.entity.HostClient;
import com.uiotsoft.util.encrypt.small.SignUtil;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;

@Slf4j
public class HostMqttController extends HostBaseController {
    private HostClient hostClient;
    private final static String model = "mqttConnect";

    public HostMqttController(HostClient hostClient) {
        this.hostClient = hostClient;
    }

    public String getHTCTopic(String type) {
        return getHTCTopic(hostClient.getSn(), model, type);
    }

    /**
     * mqtt连接
     */
    public void connect() throws MqttException {
        //mqtt连接
        getMqttConnect();
        //登录业务
        //hostOnline();
        hostLineState();
    }

    public void getMqttConnect() throws MqttException {
        log.info("++++++ 初始化成功 ++++++\n******host客户端是：{}", hostClient.toString());
        hostClient.getMyMqttClient().setMqttClient(options(), null);
        hostClient.getMyMqttClient().subscribe(hostClient.getSubTopic());
        log.info("++++++ 连接成功，订阅成功 ++++++\n******host的mqttClient是：{}", hostClient.getMyMqttClient().toString());
    }

    /**
     * 设置mqtt连接操作
     */
    public MqttConnectOptions options() {
        MqttConnectOptions options = new MqttConnectOptions();
        options.setMqttVersion(3);
        options.setUserName(hostClient.getUsername());
        options.setPassword(hostClient.getPassword().toCharArray());
        options.setConnectionTimeout(10);
        options.setKeepAliveInterval(300);
        options.setMaxReconnectDelay(5);
        options.setCleanSession(true);
        options.setAutomaticReconnect(true);
        //设置遗嘱
        String willTopic = getHTCTopic("hostLineState");
        options.setWill(willTopic, getWillData(), 1, true);
        return options;
    }

    public byte[] getWillData() {
        String lineState = "{\"sn\":\"\",\"lineState\":\"\"}";
        JSONObject lineStateJson = JSONUtil.parseObj(lineState);
        lineStateJson.put("sn", hostClient.getSn());
        lineStateJson.put("lineState", "offline");
        String willMsg = getSendData(hostClient, lineStateJson);
        byte[] willMsgByte = SignUtil.getPublishMessage(willMsg,hostClient.getMyMqttClient().getAppKey(), hostClient.getMyMqttClient().getAppSecret());
        return willMsgByte;
    }

    public void hostOnline() {
        String data = "{ \"sn\": \"\", \"oemFirm\": \"0X8001\", \"hostType\": \"sn_small\", \"hostVersion\": \"V5.01.001\", \"modelVersion\": \"V1.01.188\", \"zigbeeHardwareVersion\": \"V5.39\", \"zigbeeSoftwareVersion\": \"V5.39\", \"hostLanIp\": \"192.168.1.19\", \"hostLanPort\": 1883, \"orderVersion\": \"V1.01.103\" }";
        JSONObject dataJson = JSONUtil.parseObj(data);
        dataJson.put("sn", hostClient.getSn());
        publishToCloud(hostClient, model, "hostOnline", dataJson);
    }

    public void hostLineState() {
        String lineState = "{\"sn\":\"\",\"lineState\":\"\"}";
        JSONObject lineStateJson = JSONUtil.parseObj(lineState);
        lineStateJson.put("sn", hostClient.getSn());
        lineStateJson.put("lineState", "online");
        publishToCloud(hostClient, model, "hostLineState", lineStateJson);
    }
}
